<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class config_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function email_test()
    {
        $email = P('email', '', '邮件不能为空');
        $send = $this->module('api')->email($email, '测试标题', '当您看到此邮件，说明你的邮件配置成功！');
        if ($send) {
            $arr['error'] = '0';
            echo json_encode($arr);
        } else {
            error_json('发送失败【请确认是否配置正确】');
        }
    }
    function post()
    {
        if ($_POST) {
            $data = $_POST;
            $this->flush_config($data);
        }
    }
    public function flush_config($setsql)
    {
        foreach ($setsql as $k => $v) {
            $data['name'] = $k;
            $data['value'] = $v ? $v : '';
            $row = $this->db->get_one("select * from " . table('config') . " where name = '" . $k . "'");
            if ($row['name'] == '') {
                $this->db->add(table('config'), $data);
            } else {
                $this->db->update(table('config'), $data, "name ='{$k}'");
            }
        }
        $query = $this->db->query("select * from " . table("config"));
        while ($row = $this->db->fetch_array($query)) {
            $myDataArray[$row['name']] = $row['value'];
        }
        $query = file_put_contents('data/config.php', "<?php \r\n return " . var_export($myDataArray, true) . ";");
        if ($query) {
            right_json();
        }
    }
    public function template()
    {
        $tpl_arr = array('pc' => UQCMS_PATH . 'home/templates', 'wap' => UQCMS_PATH . 'wap/templates');
        foreach ($tpl_arr as $k => $v) {
            $tpl_ck_name = cfg('site_tpl_' . $k);
            $dir = scandir($v);
            foreach ($dir as $value) {
                if ($value != '.' && $value != '..') {
                    if ($tpl_ck_name == $value) {
                        $ck_a = array('ck' => '1');
                    } else {
                        $ck_a = array('ck' => '0');
                    }
                    $a = array('name' => $value);
                    $lt[] = array_merge($ck_a, $a);
                }
            }
            $data[] = array('key' => $k, 'list' => $lt, 'type' => $k);
            unset($lt);
            unset($k);
        }
        $this->assign('data', $data);
        $this->display();
    }
    public function temp_clear()
    {
        $allow_del = array('templates_c', 'images', 'cache', 'log', 'session', 'config');
        $file_path = isset($_GET['path']) ? $_GET['path'] : error_json('路径错误');
        if ($file_path == 'all') {
            foreach ($allow_del as $v) {
                if ($v != 'session') {
                    $this->temp_func_del($v);
                }
            }
        } else {
            $arr = explode(',', $file_path);
            foreach ($arr as $v) {
                if (in_array($v, $allow_del)) {
                    $this->temp_func_del($v);
                } else {
                    error_json('删除目标错误');
                }
            }
        }
        right_json();
    }
    private function temp_func_del($path)
    {
        if ($path == 'templates_c') {
            $compile_dir = array(UQCMS_PATH . 'temp/templates_c/admin/', UQCMS_PATH . 'temp/templates_c/home/', UQCMS_PATH . 'temp/templates_c/member/', UQCMS_PATH . 'temp/templates_c/wap/');
            foreach ($compile_dir as $v) {
                file::del_dir($v);
                file::mk_dir($v);
            }
        } else {
            file::del_dir(UQCMS_PATH . 'temp/' . $path);
            file::mk_dir(UQCMS_PATH . 'temp/' . $path);
        }
    }
}